/*
 * $Id: PropertiesFileSymbolSource.java 27 2007-04-13 01:02:48Z aaron.day $
 */
package org.crazydays.hivemind;


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.SymbolSource;

/**
 * Properties File Symbol Source
 */
public class PropertiesFileSymbolSource
	implements SymbolSource
{
	/** properties */
	private Properties properties;

	/**
	 * Set properties filename.
	 * 
	 * @param propertiesFilename Properties filename
	 */
	public void setPropertiesFilename(String propertiesFilename)
	{
		if (propertiesFilename == null) {
			throw new IllegalArgumentException(
					"propertiesFilename == null");
		}

		InputStream is = null;
		try {
			is = getClass().getResourceAsStream(propertiesFilename);
			properties = new Properties();
			properties.load(is);
		} catch (IOException e) {
			throw new ApplicationRuntimeException(
					"Unable to load properties: "
							+ propertiesFilename, e);
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
				}
			}
		}
	}

	/**
	 * Lookup value for symbol.
	 * 
	 * @param symbol Symbol identifier
	 * @return Symbol value
	 */
	public String valueForSymbol(String symbol)
	{
		if (symbol == null) {
			throw new IllegalArgumentException("symbol == null");
		}

		if (properties == null) {
			throw new ApplicationRuntimeException(
					"properties == null");
		} else {
			return properties.getProperty(symbol);
		}
	}
}
